# variables

# GRPC-TOOLS required. Install with `nuget install Grpc.Tools`.
# Then un-comment and replace [DIRECTORY] with location of files.
# For example, on macOS, you might have something like:
COMPILER=Grpc.Tools.1.14.1/tools/macosx_x64
# COMPILER=[DIRECTORY]

#path to ts plugin
GEN_TS_PATH=./node_modules/.bin/protoc-gen-ts
# Path to the grpc_node_plugin
PROTOC_GEN_GRPC_PATH="./node_modules/.bin/grpc_tools_node_protoc_plugin"

SRC_DIR=proto/mlagents_envs/communicator_objects
SRC_DIR_ONNX=proto/onnx
DST_DIR_C=../project/extensions/mlagents/static/assets/Runtime/Grpc/CommunicatorObjects
DST_DIR_T=../project/extensions/mlagents/static/assets/ONNX/Generated
DST_DIR_P=../ml-agents-envs
PROTO_PATH=proto
PYTHON_PACKAGE=mlagents_envs/communicator_objects

# clean
rm -rf $DST_DIR_C
rm -rf $DST_DIR_P/$PYTHON_PACKAGE
mkdir -p $DST_DIR_C
mkdir -p $DST_DIR_P/$PYTHON_PACKAGE

# generate proto objects in python and TS

$COMPILER/protoc --proto_path=proto --python_out=$DST_DIR_P --mypy_out=$DST_DIR_P $SRC_DIR/*.proto
$COMPILER/protoc --proto_path=proto --plugin=protoc-gen-ts=$GEN_TS_PATH --js_out=import_style=commonjs,binary:$DST_DIR_C --ts_out=$DST_DIR_C $SRC_DIR/*.proto
$COMPILER/protoc --proto_path=proto --plugin=protoc-gen-ts=$GEN_TS_PATH --js_out=import_style=commonjs,binary:$DST_DIR_T --ts_out=$DST_DIR_T $SRC_DIR_ONNX/onnx.proto

# grpc
GRPC=unity_to_external.proto

python -m grpc_tools.protoc --proto_path=proto --python_out=$DST_DIR_P --grpc_python_out=$DST_DIR_P $SRC_DIR/$GRPC
$COMPILER/protoc --proto_path=proto --plugin=protoc-gen-ts=$GEN_TS_PATH --js_out=import_style=commonjs,binary:$DST_DIR_C --ts_out=service=grpc-web:$DST_DIR_C $SRC_DIR/$GRPC


# Generate the init file for the python module
# rm -f $DST_DIR_P/$PYTHON_PACKAGE/__init__.py
for FILE in $DST_DIR_P/$PYTHON_PACKAGE/*.py
do
FILE=${FILE##*/}
# echo from .$(basename $FILE) import \* >> $DST_DIR_P/$PYTHON_PACKAGE/__init__.py
echo from .${FILE%.py} import \* >> $DST_DIR_P/$PYTHON_PACKAGE/__init__.py
done

# Surround UnityToExternal.cs file with macro
# echo "#if UNITY_EDITOR || UNITY_STANDALONE
# #define MLA_SUPPORTED_TRAINING_PLATFORM
# #endif
# #if MLA_SUPPORTED_TRAINING_PLATFORM
# `cat $DST_DIR_C/UnityToExternalGrpc.cs`
# #endif" > $DST_DIR_C/UnityToExternalGrpc.cs

# Remove the __init__.py file since it is not needed
rm $DST_DIR_P/$PYTHON_PACKAGE/__init__.py
touch $DST_DIR_P/$PYTHON_PACKAGE/__init__.py
